﻿using STHPMS1.Common.C_Definition;
using STHPMS1.Common.C_Entity;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ZZHMHN.Common.C_Attribute;

namespace STHPMS1.Common.C_Extension
{
    public static class UrlExt
    {
        private static ConcurrentDictionary<int, NavigateAttribute> _attrList = new ConcurrentDictionary<int, NavigateAttribute>();

	
        static UrlExt()
        {
            foreach (FieldInfo field in typeof(UrlDefinition).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))
            {
                if(field.IsLiteral && !field.IsInitOnly)
                {
                     NavigateAttribute attr =Attribute.GetCustomAttribute(field,typeof(NavigateAttribute)) as NavigateAttribute;

                    if (attr != null)
                    {             
                        int key=field.GetRawConstantValue().GetHashCode();
                        _attrList.TryAdd(key, attr);
                    }
                }
               
                
            }
        }
        public static string GetPath(this string key, int index = 0)
        {
            NavigateAttribute attr=null;
            if(_attrList.TryGetValue(key.GetHashCode(),out attr))
            {
                if(attr.Path.Length>index)
                {
                    return attr.Path[index];
                }
            }
            return null;
        }
    }
}
